package com.ang.jdk8.stream;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ssang
 * @date 2023-03-01 9:12
 */
public class MapTest {
    public static void main(String[] args) {
        MapTest test = new MapTest();
        test.test01();
    }

    /**
     * 合并map
     */
    public void test01(){
        Map<String, String> map1 = new HashMap<String, String>(){{
            put("a","A");
            put("b","B");
            put("c","C");
        }};
        Map<String, String> map2 = new HashMap<String, String>(){{
            put("e","E");
            put("f","F");
            put("c","C1");
        }};

        //map2合并到map1中
        map2.forEach((k,v)->{
            //k1和k2重复的情况，k1是获取map1中，k2是获取map2中
            map1.merge(k,v,(k1,k2)->k2);  //c:C1
//            map1.merge(k,v,(k1,k2)->k1);    //c:C
        });

        map1.forEach((k,v)->{
            System.out.println(k+":"+v);
        });

        System.out.println("=============");

        //map2没有变化
        map2.forEach((k,v)->{
            System.out.println(k+":"+v);
        });
    }

}
